package com.hhh.api.module.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.hhh.api.module.entity.BlackList;
import com.hhh.api.module.service.BlackListService;
import com.hhh.api.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 黑名单 前端控制器
 * </p>
 *
 * @author hhh
 * @since 2024-12-30
 */
@Api(tags = "自定义黑名单")
@RestController
@RequestMapping("/black-list")
public class BlackListController {

    @Resource
    private BlackListService blackListService;

    @Resource
    private SensitiveWordBs sensitiveWordBs;

    @ApiOperation(value = "分页获取")
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10") Integer pageSize){
        Page<BlackList> page = new Page<>(pageNo, pageSize);
        IPage<BlackList> iPage = blackListService.page(page);
        return Result.ok(iPage);
    }

    @ApiOperation(value = "单个新增/修改")
    @PostMapping("/edit")
    public Result edit(@RequestBody BlackList blackList){
        sensitiveWordBs.addWord(blackList.getWord());
        return Result.ok(blackListService.saveOrUpdate(blackList));
    }

    @ApiOperation(value = "单个删除")
    @DeleteMapping("/del")
    public Result edit(@RequestParam Integer id){
        String word = blackListService.getById(id).getWord();
        sensitiveWordBs.removeWord(word);
        return Result.ok(blackListService.removeById(id));
    }
}
